﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using log4net;
using Common;
using CashFlow.Db.Entity;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form._02_User
{
    public partial class ResetPwdForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(ResetPwdForm));

        public int UserId { get; set; }
        private UserInfoEntity UserInfo { get; set; }

        public ResetPwdForm()
        {
            InitializeComponent();
        }

        private void ResetPwdForm_Load(object sender, EventArgs e)
        {

        }

        private void ResetPwdForm_Shown(object sender, EventArgs e)
        {
            try
            {
                var ui = UserService.QueryByUserId(UserId);
                UserInfo = ui.UserInfo;
                label2.Text = UserInfo.Name;
                label4.Text = UserInfo.Phone;
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog("网络错误！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                if (UICommonTool.ShowConfirmDialog("确定要重置密码吗？"))
                {
                    UserInfo.Password = Md5EncryptionTool.Encrypt(textBox1.Text.Trim(), true);
                    try
                    {
                        bool b = UserService.ResetPwd(UserInfo);
                        if (b)
                        {
                            UICommonTool.ShowInfoDialog("重置成功！");
                            DialogResult = DialogResult.OK;
                        }
                        else
                        {
                            UICommonTool.ShowAlertDialog("重置失败！");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex);
                        UICommonTool.ShowAlertDialog("网络错误！");
                    }
                }
            }
        }

        private bool ValidateForm()
        {
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                UICommonTool.ShowAlertDialog("新密码不能为空！");
                return false;
            }

            var reg = new Regex(@"\w{6,15}");
            if (!reg.IsMatch(textBox1.Text.Trim()))
            {
                UICommonTool.ShowAlertDialog("新密码格式不正确！");
                return false;
            }

            if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                UICommonTool.ShowAlertDialog("确认新密码不能为空！");
                return false;
            }

            var reg2 = new Regex(@"\w{6,15}");
            if (!reg2.IsMatch(textBox2.Text.Trim()))
            {
                UICommonTool.ShowAlertDialog("确认新密码格式不正确！");
                return false;
            }

            if (textBox2.Text.Trim() != textBox1.Text.Trim())
            {
                UICommonTool.ShowAlertDialog("新密码和确认新密码不一致！");
                return false;
            }

            return true;
        }
    }
}
